/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ie.nell;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import ie.nell.chart.ClicksChart;

/**
 *
 * @author sweibelzahl
 */
public class Stats extends Activity {

    private SQLiteDatabase db;
    private Cursor cursor;
    private TextView eventStats;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stats);
        eventStats = (TextView) findViewById(R.id.stats);

        // event summary
        db = ApplicationSettings.db;
        String statsSummary = "";
        int count;
        cursor = db.query(EventDBOpenHelper.EVENT_TABLE_NAME, new String[]{EventDBOpenHelper.EVENT_TYPE, EventDBOpenHelper.TIME},
                "type=1", null, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            statsSummary = count + " clicks so far \n";

            if (count > 0) {
                cursor.moveToFirst();
                statsSummary += " first click at " + cursor.getString(1) + "\n";
                cursor.moveToLast();
                statsSummary += " last click at " + cursor.getString(1) + "\n";
            }
        }
        cursor.close();
        // page summary
        cursor = db.query(EventDBOpenHelper.URL_TABLE_NAME, new String[]{EventDBOpenHelper.TIME},
                null, null, null, null, null);
        if (cursor != null) {
            count = cursor.getCount();
            statsSummary += count + " pages visited so far \n";
            if (count > 0) {
                cursor.moveToFirst();
                statsSummary += " first page at " + cursor.getString(0) + "\n";
                cursor.moveToLast();
                statsSummary += " last page at " + cursor.getString(0) + "\n";
            }
        }
        if (statsSummary.equals("")) {
            statsSummary = "no data collected yet";
        }
        eventStats.setText(statsSummary);
        cursor.close();
    }

    public void showChart24h(View view) {
        ClicksChart clickChart = new ClicksChart(24, "hours");
        Intent i = clickChart.execute(this);
        startActivity(i);
    }
    
    public void showChart60min(View view) {
        ClicksChart clickChart = new ClicksChart(60, "minutes");
        Intent i = clickChart.execute(this);
        startActivity(i);
    }

    public void showChart7d(View view) {
        ClicksChart clickChart = new ClicksChart(7, "days");
        Intent i = clickChart.execute(this);
        startActivity(i);
    }
}
